package logic

import (
	"iot-base/auth/model"
	"iot-base/auth/svc"
)

type UserRoleLogic struct {
}

func NewUserRoleLogic() *UserRoleLogic {
	var _userRole UserRoleLogic
	return &_userRole
}

// AddUserRole 添加用户角色. (todo 已废弃)
func (l *UserRoleLogic) AddUserRole(uid int64, roleId int64) error {
	var userRole model.UserRole
	userRole.Uid = uid
	userRole.RoleId = roleId
	return svc.Dao.TUserRole.AddUserRole(&userRole)
}

// DelUserRole 删除用户角色. (todo 已废弃)
func (l *UserRoleLogic) DelUserRole(uid int64, roleId int64) error {
	_, err := svc.Dao.TUserRole.DelUserRole(uid, roleId)
	return err
}

// EditUserRole 编辑用户角色. (todo 已废弃)
func (l *UserRoleLogic) EditUserRole(uid int64, roleIds []int64) error {
	_, err := svc.Dao.TUserRole.DelUserRole(uid, 0)
	if err != nil {
		return err
	}
	length := len(roleIds)
	if length == 0 {
		return nil
	}
	userRoles := make([]model.UserRole, 0, length)
	for _, id := range roleIds {
		data := model.UserRole{
			Uid:    uid,
			RoleId: id,
		}
		userRoles = append(userRoles, data)
	}
	return svc.Dao.TUserRole.BatchSave(userRoles)
}

// FindUserRole 查询用户角色.
func (l *UserRoleLogic) FindUserRole(uid int64) ([]model.AuthRole, error) {
	return svc.Dao.TAuthRole.GetRole(uid, 0)
}
